<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="batchSign"
        >一键签到</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="updateCookie"
        >更新COOKIE</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="wordsList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="OID" align="center" prop="oid" />
      <el-table-column label="超话名称" align="center" prop="title" />
      <el-table-column label="介绍" align="center" prop="intro" width="200" >
        <template slot-scope="scope">
          <div class="cell-content">
            <el-tooltip :content="scope.row.intro" placement="top" effect="dark" >
              <span class="content-text">{{ scope.row.intro }}</span>
            </el-tooltip>
            <br>
            <el-button
              type="text"
              icon="el-icon-copy-document"
              @click="copyText(scope.row.intro)"
              style="margin-left: 5px;"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="pic" />
      <el-table-column label="最新状态" align="center" prop="latestStatus" width="200" >
        <template slot-scope="scope">
          <div class="cell-content">
            <el-tooltip :content="scope.row.latestStatus" placement="top" effect="dark" >
              <span class="content-text">{{ scope.row.latestStatus }}</span>
            </el-tooltip>
            <br>
            <el-button
              type="text"
              icon="el-icon-copy-document"
              @click="copyText(scope.row.latestStatus)"
              style="margin-left: 5px;"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态计数" align="center" prop="statusCount" />
      <el-table-column label="关注计数" align="center" prop="followCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.ifSign"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="superWordsSign(scope.row)"
          >签到</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--添加微博cookie-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微博令牌" prop="cookie">
          <el-input v-model="form.cookie" placeholder="请输入微博令牌" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWords, batchSign, signTopic, setCookie, verifyCookie } from "@/api/kiku/words";

export default {
  name: "Words",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 我的超话表格数据
      wordsList: [],
      // 弹出层超话名称
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.verifyWeiboCookie();
  },
  methods: {
    /**复制功能*/
    copyText(content) {
      navigator.clipboard.writeText(content).then(() => {
        this.$message.success('内容已复制到剪贴板！');
      }).catch(err => {
        this.$message.error('复制失败，请重试！');
      });
    },
    updateCookie() {
      this.open = true;
    },
    /** 查询我的超话列表 */
    verifyWeiboCookie() {
      verifyCookie().then(res => {
        if(res.data===618){
          this.$modal.msgSuccess("验证通过");
          // 查询列表
          this.getList();
        }else{
          this.$modal.msgError("验证失败，请重新输入令牌");
          this.open = true;
        }
      });
    },
    getList() {
      this.loading = true;
      listWords().then(response => {
        this.wordsList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cookie:null
      };
      this.resetForm("form");
    },

    /** 一键签到 */
    batchSign() {
      this.$modal.confirm("是否一键签到所有超话？").then(() => {
        const loadingInstance = this.$loading({
          lock: true,
          text: "小橘子等一下马上好了",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.3)",
          customClass: "orange-loading-text"
        });
        batchSign().then(res => {
          this.$modal.msgSuccess(res.data);
          this.getList();
        }).catch(err => {
          this.$modal.msgError(err.msg);
        }).finally(() => {
          loadingInstance.close();
        });
      });
    },
    /** 签到 */
    superWordsSign(row) {
      const oid = row.oid;
      const title = row.title;
      this.$modal.confirm(`是否签到超话：${title}？`).then(() => {
        signTopic(oid,title).then(res=>{
          this.$modal.msgSuccess(res.msg);
          this.getList();
        }).catch(err => {
          this.$modal.msgError(err.msg);
        })
      })
    },
    /** 提交按钮 */
    submitForm() {
      setCookie(this.form.cookie).then(res => {
        this.$modal.msgSuccess("设置成功");
        this.open = false;
        this.getList();
      });
    },

  }
};
</script>
<style scoped>
.orange-loading-text .el-loading-text {
  color: #FFA500; /* 橘子色 */
}
.content-text {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: top;
}
</style>
